package com.zretc.emptraining.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.zretc.emptraining.dbtools.PageData;
import com.zretc.emptraining.dbtools.SensitivewordFilter;
import com.zretc.emptraining.entity.CourseEva;

import com.zretc.emptraining.entity.TeacherEva;
import com.zretc.emptraining.service.CourseEvaService;

import com.zretc.emptraining.service.TeacherEvaService;
import com.zretc.emptraining.serviceimpl.CourseEvaServiceImpl;

import com.zretc.emptraining.serviceimpl.TeacherEvaServiceImpl;

/**
 * Servlet implementation class CourseEvaServlet
 */
@WebServlet("/CourseEvaServlet")
public class CourseEvaServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	// 创建对象
	CourseEvaService courseEvaService = new CourseEvaServiceImpl();
	// 创建对象
	TeacherEvaService teacherEvaService = new TeacherEvaServiceImpl();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CourseEvaServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// 设置请求编码
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("gbk");
		// 判断请求内容
		String op = "";
		if (request.getParameter("op") != null) {
			op = request.getParameter("op");
		}
		if ("".equals(op)) {
			// 查询所有
			List<CourseEva> list = courseEvaService.getCourseEvaAll();
			// 转发
			request.setAttribute("list", list);
			request.getRequestDispatcher("CourseEvaList.jsp").forward(request, response);
		} else if ("add".equals(op)) {
			// 添加课程评价
			addCourseEva(request, response);
		} else if ("queryByPage".equals(op)) {
			doQuery(request, response);
		} else if ("upd".equals(op)) {
			updCourseEva(request, response);
		} else if ("queryByCpNo".equals(op)) {
			queryByCpNo(request, response);
		} else if ("queryByCpNoAjax".equals(op)) {
			queryByCpNoAjax(request, response);
		} else if ("teaQueryByCpNo".equals(op)) {
			teaQueryByCpNo(request, response);
		}
	}

	private void teaQueryByCpNo(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String courseName = request.getParameter("courseName");
		String cpNoString = request.getParameter("cpNo");
		String coursePic = request.getParameter("coursePic");
		String courseStatus = request.getParameter("courseStatus");
		String subJect = request.getParameter("subJect");
		String evalContent = request.getParameter("evalContent");
		String teacherPic = request.getParameter("teacherPic");
		String teacherNo = request.getParameter("teacherNo");
		String teacherName = request.getParameter("teacherName");
		String courseNum = request.getParameter("courseNum");
		String courseDesc = request.getParameter("courseDesc");
		String courseStart = request.getParameter("courseStart");
		String courseEnd = request.getParameter("courseEnd");
		String courseTime = request.getParameter("courseTime");
		String coursePlace = request.getParameter("coursePlace");
		String teacherXl = request.getParameter("teacherXl");
		String teacherSex = request.getParameter("teacherSex");
		String teacherTel = request.getParameter("teacherTel");
		String teacherDesc = request.getParameter("teacherDesc");

		// 当前页
		int page = 1;
		// 每页的记录数
		int pageSize = 7;
		if (request.getParameter("page") != null) {
			page = Integer.parseInt(request.getParameter("page"));
		}
		if (request.getParameter("pageSize") != null) {
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}
		// 调用查询的方法
		PageData<CourseEva> pd = courseEvaService.getCourseEvaByCpNo(page, pageSize, 1);


		// 存储值
		request.setAttribute("pd", pd);
		request.setAttribute("courseName", courseName);
		request.setAttribute("cpNoString", cpNoString);
		request.setAttribute("coursePic", coursePic);
		request.setAttribute("courseStatus", courseStatus);
		request.setAttribute("subJect", subJect);
		request.setAttribute("evalContent", evalContent);
		request.setAttribute("teacherPic", teacherPic);
		request.setAttribute("teacherNo", teacherNo);
		request.setAttribute("teacherName", teacherName);
		request.setAttribute("courseNum", courseNum);
		request.setAttribute("courseDesc", courseDesc);
		request.setAttribute("courseStart", courseStart);
		request.setAttribute("courseEnd", courseEnd);
		request.setAttribute("courseTime", courseTime);
		request.setAttribute("coursePlace", coursePlace);
		request.setAttribute("teacherXl", teacherXl);
		request.setAttribute("teacherSex", teacherSex);
		request.setAttribute("teacherTel", teacherTel);
		request.setAttribute("teacherDesc", teacherDesc);

		// 转发
		request.getRequestDispatcher("TeacherCoursesEva.jsp").forward(request, response);
	}

	private void queryByCpNoAjax(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("application/json;charset=utf-8");

		int cpNo = Integer.parseInt(request.getParameter("cpNo"));
		// 当前页
		int page = 1;
		// 每页的记录数
		int pageSize = 6;
		if (request.getParameter("page") != null) {
			page = Integer.parseInt(request.getParameter("page"));

		}
		if (request.getParameter("pageSize") != null) {
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}
		// 调用查询的方法
		PageData<CourseEva> pd = courseEvaService.getCourseEvaByCpNo(page, pageSize, cpNo);
	
		// 敏感词过滤器
		SensitivewordFilter filter = new SensitivewordFilter();
		// 匿名处理
		for (int i = 0; i < pd.getData().size(); i++) {
			String string = pd.getData().get(i).getEvalContent();
			// 替换敏感词
			String string2 = filter.replaceSensitiveWord(string, 2, "*");
			pd.getData().get(i).setEvalContent(string2);
			// 匿名处理
			if (pd.getData().get(i).getEvalState() == 2) {

				if (pd.getData().get(i).getEmpName().length() == 2) {
					String name = pd.getData().get(i).getEmpName().substring(0, 1) + "*";
					pd.getData().get(i).setEmpName(name);
				} else if (pd.getData().get(i).getEmpName().length() == 3) {

					String name = pd.getData().get(i).getEmpName().substring(0, 1) + "**";
					pd.getData().get(i).setEmpName(name);
				} else if (pd.getData().get(i).getEmpName().length() == 4) {

					String name = pd.getData().get(i).getEmpName().substring(0, 2) + "**";
					pd.getData().get(i).setEmpName(name);
				}
			}

		}

		// 创建gson
		Gson gson = new Gson();
		String jsonString = gson.toJson(pd);
		PrintWriter out = response.getWriter();
		// 打印
		out.print(jsonString);
		// 关闭
		out.close();
	}

	private void queryByCpNo(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String courseName = request.getParameter("courseName");
		String cpNoString = request.getParameter("cpNo");
		String coursePic = request.getParameter("coursePic");
		String courseStatus = request.getParameter("courseStatus");
		String subJect = request.getParameter("subJect");
		String evalContent = request.getParameter("evalContent");
		String teacherPic = request.getParameter("teacherPic");
		String teacherName = request.getParameter("teacherName");
		String courseNum = request.getParameter("courseNum");
		String courseDesc = request.getParameter("courseDesc");
		String courseStart = request.getParameter("courseStart");
		String courseEnd = request.getParameter("courseEnd");
		String courseTime = request.getParameter("courseTime");
		String coursePlace = request.getParameter("coursePlace");
		String teacherXl = request.getParameter("teacherXl");
		String teacherSex = request.getParameter("teacherSex");
		String teacherTel = request.getParameter("teacherTel");
		String teacherDesc = request.getParameter("teacherDesc");

		// 当前页
		int page = 1;
		// 每页的记录数
		int pageSize = 7;
		if (request.getParameter("page") != null) {
			page = Integer.parseInt(request.getParameter("page"));
		}
		if (request.getParameter("pageSize") != null) {
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}
		// 调用查询的方法
		PageData<CourseEva> pd = courseEvaService.getCourseEvaByCpNo(page, pageSize, 1);

		// 取值
		int empNo = Integer.parseInt(request.getParameter("empNo"));
		int cpNo = Integer.parseInt(cpNoString);

		// 创建对象
		TeacherEvaService teacherEvaService = new TeacherEvaServiceImpl();
		// 添加课程
		List<TeacherEva> list = teacherEvaService.doTeacherEva(empNo, cpNo);
		// 标志

		TeacherEva msg = null;
		if (list.size() != 0) {
			msg = list.get(0);

		}

		String msgs = "0";

		// 判断是否有这个分数
		if (msg != null) {
			msgs = Double.toString(msg.getEvalScore());

		}
		System.out.println(msgs);
		// 存储值
		request.setAttribute("msgs", msgs);

		// 存储值
		request.setAttribute("pd", pd);
		request.setAttribute("courseName", courseName);
		request.setAttribute("cpNoString", cpNoString);
		request.setAttribute("coursePic", coursePic);
		request.setAttribute("courseStatus", courseStatus);
		request.setAttribute("subJect", subJect);
		request.setAttribute("evalContent", evalContent);
		request.setAttribute("teacherPic", teacherPic);
		request.setAttribute("teacherName", teacherName);
		request.setAttribute("courseNum", courseNum);
		request.setAttribute("courseDesc", courseDesc);
		request.setAttribute("courseStart", courseStart);
		request.setAttribute("courseEnd", courseEnd);
		request.setAttribute("courseTime", courseTime);
		request.setAttribute("coursePlace", coursePlace);
		request.setAttribute("teacherXl", teacherXl);
		request.setAttribute("teacherSex", teacherSex);
		request.setAttribute("teacherTel", teacherTel);
		request.setAttribute("teacherDesc", teacherDesc);

		// 转发
		request.getRequestDispatcher("SingleCoursesEva.jsp").forward(request, response);
	}

	private void updCourseEva(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("application/json;charset=utf-8");
		// 获取文本框里的值
		int evalNo = 0;
		if (request.getParameter("evalNo") != "") {
			evalNo = Integer.parseInt(request.getParameter("evalNo"));
		}
		int evalState = 0;
		if (request.getParameter("evalState") != "") {
			evalState = Integer.parseInt(request.getParameter("evalState"));
		}

		CourseEva courseEva = new CourseEva(evalNo, 0, 0, "", "", evalState);

		// 修改状态
		boolean flag = courseEvaService.updCourseEva(courseEva);
//			//创建gson
		Gson gson = new Gson();
		String jsonString = gson.toJson(flag);
		PrintWriter out = response.getWriter();
		// 打印
		out.print(jsonString);
		// 关闭
		out.close();
	}

	private void addCourseEva(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("application/json;charset=utf-8");
		// 调用添加课程评价的方法
		// 获取文本框里的值
		int empNo = Integer.parseInt(request.getParameter("empNo"));
		int cpNo = Integer.parseInt(request.getParameter("cpNo"));
		System.out.println(cpNo);
		String evalContent = request.getParameter("evalContent");
		int evalState = Integer.parseInt(request.getParameter("evalState"));
		// 敏感词过滤器
		SensitivewordFilter filter = new SensitivewordFilter();
		String string2 = filter.replaceSensitiveWord(evalContent, 2, "*");
		CourseEva courseEva = new CourseEva(0, empNo, cpNo, "", string2, evalState);
		// 添加课程
		boolean flag = courseEvaService.addCourseEva(courseEva);
		// 创建gson
		Gson gson = new Gson();
		String jsonString = gson.toJson(flag);
		PrintWriter out = response.getWriter();
		// 打印
		out.print(jsonString);
		// 关闭
		out.close();
	}

	private void doQuery(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 获取查询的内容
		String courseNoQuery = "";
		if (request.getParameter("courseNoQuery") != null) {
			courseNoQuery = request.getParameter("courseNoQuery");
		}
		// 当前页
		int page = 1;
		// 每页的记录数
		int pageSize = 7;
		if (request.getParameter("page") != null) {
			page = Integer.parseInt(request.getParameter("page"));
		}
		if (request.getParameter("pageSize") != null) {
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}
		// 调用查询的方法
		PageData<CourseEva> pd = courseEvaService.getCourseEvaByPage(page, pageSize, courseNoQuery);

		// 存储值
		request.setAttribute("pd", pd);
		request.setAttribute("courseNoQuery", courseNoQuery);
		// 转发
		request.getRequestDispatcher("CourseEvaList.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
